package com.yanqu.road.entity.activity.snowgarden.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class SnowGardenConfig {

    // 活动信息
    private ActivityInfo activityInfo;

    private Random random = new Random();

    // 游园积分
    private int score;
    private int score1;//(二阶段积分)

    // 建筑材料
    private int buildItemId;
    // 建筑材料每次抽取消耗
    private int buildItemCost;
    // 建筑材料回收比例
    @Deprecated
    private int buildItemRecycle;

    // 游园票
    private int visitItemId;
    // 游园票每次抽取消耗
    private int visitItemCost;
    // 游园票回收比例
    @Deprecated
    private int visitItemRecycle;

    // 玩家无商会协助事件4积分
    private int noUnionComb4Score;
    // 玩家无商会协助事件10积分
    private int noUnionComb10Score;
    // 游客最多存在数量
    private int visitCountMax;

    // 初始化默认服装id
    private int initSuitId;

    // 触发事件保底参数
    private int combMinParam1 = 9999;     // 阶段1下限
    private int combMaxParam1 = 9999;     // 阶段1上限
    private int combMinParam2 = 9999;     // 阶段2下限
    private int combMaxParam2 = 9999;     // 阶段2上限
    private String visitorParam;   // 事件随机门客的品质概率

    // 初始骰子点数投掷模板
    private List<List<Integer>> dice1; // 一阶段
    private List<List<Integer>> dice2; // 二阶段

    // 投掷随机奖励
    private String rollReward; // 一阶段
    private String rollReward2; // 二阶段

    private List<List<Property>> rollRewardList = new ArrayList<>();
    private List<List<Property>> rollRewardList2 = new ArrayList<>();

    // 阶段开始结束时间
    private long period1StartTime; // 一阶段开始
    private long period1EndTime; // 一阶段结束
    private long period2StartTime; // 二阶段开始
    private long period2EndTime; // 二阶段结束


    private Map<Integer, SnowGardenCombConfig> snowGardenCombConfig;

    private Map<Integer, SnowGardenDollConfig> snowGardenDollConfig;

    private Map<Integer, SnowGardenLevelConfig> snowGardenLevelConfig;

    private Map<Integer, SnowGardenMapConfig> snowGardenMapConfig;

    private Map<Integer, SnowGardenStatueConfig> snowGardenStatueConfig;

    //key:id
    private Map<Integer, SnowGardenVisitorConfig> snowGardenVisitorConfig;

    private Map<Integer, SnowGardenShowConfig> snowGardenShowConfig;

    //品质-权重
    private Map<Integer, Integer> randomShowMap = new HashMap<>();
    //key:品质 value: ids
    private Map<Integer, List<Integer>> qualityIdListMap = new HashMap<>();

    public void initActivityConfig(Map<String, ActivityConfig> configMap) {
        // 积分
        setScore(configMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue());
        setScore1(configMap.get("EXCHANGE_SCORE_ITEM_ID_1").getIntValue());
        // 道具ID
        setBuildItemId(configMap.get("SNOW_GARDEN_ENERGY_ITEM_ID_1").getIntValue());// 建筑材料
        setVisitItemId(configMap.get("SNOW_GARDEN_ENERGY_ITEM_ID_2").getIntValue());// 游园票
        // 默认服装
        setInitSuitId(configMap.get("SNOW_GARDEN_INITIAL_DOLL_ID").getIntValue());
        // 道具消耗
        setBuildItemCost(configMap.get("SNOW_GARDEN_PERIOD_1_CONSUMPTION").getIntValue());// 道具ID
        setVisitItemCost(configMap.get("SNOW_GARDEN_PERIOD_2_CONSUMPTION").getIntValue());// 游园票
        // 道具回收值
//        setBuildItemRecycle(configMap.get("SNOW_GARDEN_ENERGY_ITEM_RECYCLE_1").getIntValue());// 道具ID回收比例
//        setVisitItemRecycle(configMap.get("SNOW_GARDEN_ENERGY_ITEM_RECYCLE_2").getIntValue());// 游园票回收比例
        // 无商会玩家获取积分
        setNoUnionComb4Score(configMap.get("SNOW_GARDEN_SINGLE_PLAYER_4").getIntValue());
        setNoUnionComb10Score(configMap.get("SNOW_GARDEN_SINGLE_PLAYER_10").getIntValue());
        // 游客数量
        setVisitCountMax(configMap.get("SNOW_GARDEN_VISITOR_MAX").getIntValue());
        // 阶段保底参数
        List<Integer> param1 = configMap.get("SNOW_GARDEN_COMB_PARAM_1").getIntListValue(";");
        if(param1.size() == 2){
            setCombMinParam1(param1.get(0));
            setCombMaxParam1(param1.get(1));
        }
        // 阶段保底参数
        List<Integer> param2 = configMap.get("SNOW_GARDEN_COMB_PARAM_2").getIntListValue(";");
        if(param2.size() == 2){
            setCombMinParam2(param2.get(0));
            setCombMaxParam2(param2.get(1));
        }
        setVisitorParam(configMap.get("SNOW_GARDEN_VISITOR_PARAM").getValue());

        // 初始投掷模板
        List<List<Integer>> initialDice1 = new ArrayList<>();
        for (String str1 : configMap.get("SNOW_GARDEN_INITIAL_DICE_1").getStringListValue("\\|")) {
            initialDice1.add(StringUtils.stringToIntegerList(str1, ","));
        }
        setDice1(initialDice1);
        List<List<Integer>> initialDice2 = new ArrayList<>();
        for (String str2 : configMap.get("SNOW_GARDEN_INITIAL_DICE_2").getStringListValue("\\|")) {
            initialDice2.add(StringUtils.stringToIntegerList(str2, ","));
        }
        setDice2(initialDice2);

        setRollReward(configMap.get("SNOW_GARDEN_DICE_ROLL_REWARD").getValue());
        setRollReward2(configMap.get("SNOW_GARDEN_DICE_ROLL_REWARD_2").getValue());

        // 解析参数3
        // 解析第一阶段，开始时间=活动开始时间  结束时间=活动开始时间+一阶段持续时间
        long startPeriod1Time = getActivityInfo().getBeginTime() * 1000;
        setPeriod1StartTime(startPeriod1Time);
        int period1NeedHours = configMap.get("SNOW_GARDEN_END_TIME_1").getIntValue();// 单位:小时
        setPeriod1EndTime(startPeriod1Time + period1NeedHours * 3600 * 1000);
        // 解析第二阶段,开始时间=活动开始时间+二阶段开始时间  结束时间=活动结束时间
        int period2NeedHours = configMap.get("SNOW_GARDEN_BEGIN_TIME_2").getIntValue();// 单位:小时
        setPeriod2StartTime(startPeriod1Time + period2NeedHours * 3600 * 1000);
        setPeriod2EndTime(getActivityInfo().getEndTime() * 1000);
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getScore1() {
        return score1;
    }

    public void setScore1(int score1) {
        this.score1 = score1;
    }

    public int getBuildItemId() {
        return buildItemId;
    }

    public void setBuildItemId(int buildItemId) {
        this.buildItemId = buildItemId;
    }

    public int getBuildItemCost() {
        return buildItemCost;
    }

    public void setBuildItemCost(int buildItemCost) {
        this.buildItemCost = buildItemCost;
    }

    public int getBuildItemRecycle() {
        return buildItemRecycle;
    }

    public void setBuildItemRecycle(int buildItemRecycle) {
        this.buildItemRecycle = buildItemRecycle;
    }

    public int getVisitItemId() {
        return visitItemId;
    }

    public void setVisitItemId(int visitItemId) {
        this.visitItemId = visitItemId;
    }

    public int getVisitItemCost() {
        return visitItemCost;
    }

    public void setVisitItemCost(int visitItemCost) {
        this.visitItemCost = visitItemCost;
    }

    public int getVisitItemRecycle() {
        return visitItemRecycle;
    }

    public void setVisitItemRecycle(int visitItemRecycle) {
        this.visitItemRecycle = visitItemRecycle;
    }

    public int getInitSuitId() {
        return initSuitId;
    }

    public void setInitSuitId(int initSuitId) {
        this.initSuitId = initSuitId;
    }

    public int getNoUnionComb4Score() {
        return noUnionComb4Score;
    }

    public void setNoUnionComb4Score(int noUnionComb4Score) {
        this.noUnionComb4Score = noUnionComb4Score;
    }

    public int getNoUnionComb10Score() {
        return noUnionComb10Score;
    }

    public void setNoUnionComb10Score(int noUnionComb10Score) {
        this.noUnionComb10Score = noUnionComb10Score;
    }

    public int getVisitCountMax() {
        return visitCountMax;
    }

    public void setVisitCountMax(int visitCountMax) {
        this.visitCountMax = visitCountMax;
    }

    public int getCombMinParam1() {
        return combMinParam1;
    }

    public void setCombMinParam1(int combMinParam1) {
        this.combMinParam1 = combMinParam1;
    }

    public int getCombMaxParam1() {
        return combMaxParam1;
    }

    public void setCombMaxParam1(int combMaxParam1) {
        this.combMaxParam1 = combMaxParam1;
    }

    public int getCombMinParam2() {
        return combMinParam2;
    }

    public void setCombMinParam2(int combMinParam2) {
        this.combMinParam2 = combMinParam2;
    }

    public int getCombMaxParam2() {
        return combMaxParam2;
    }

    public void setCombMaxParam2(int combMaxParam2) {
        this.combMaxParam2 = combMaxParam2;
    }

    public String getVisitorParam() {
        return visitorParam;
    }

    public void setVisitorParam(String visitorParam) {
        this.visitorParam = visitorParam;
    }

    public Map<Integer, SnowGardenCombConfig> getSnowGardenCombConfig() {
        return snowGardenCombConfig;
    }

    public void setSnowGardenCombConfig(Map<Integer, SnowGardenCombConfig> snowGardenCombConfig) {
        this.snowGardenCombConfig = snowGardenCombConfig;
    }

    public Map<Integer, SnowGardenDollConfig> getSnowGardenDollConfig() {
        return snowGardenDollConfig;
    }

    public void setSnowGardenDollConfig(Map<Integer, SnowGardenDollConfig> snowGardenDollConfig) {
        this.snowGardenDollConfig = snowGardenDollConfig;
    }

    public Map<Integer, SnowGardenLevelConfig> getSnowGardenLevelConfig() {
        return snowGardenLevelConfig;
    }

    public void setSnowGardenLevelConfig(Map<Integer, SnowGardenLevelConfig> snowGardenLevelConfig) {
        this.snowGardenLevelConfig = snowGardenLevelConfig;
    }

    public Map<Integer, SnowGardenMapConfig> getSnowGardenMapConfig() {
        return snowGardenMapConfig;
    }

    public void setSnowGardenMapConfig(Map<Integer, SnowGardenMapConfig> snowGardenMapConfig) {
        this.snowGardenMapConfig = snowGardenMapConfig;
    }

    public Map<Integer, SnowGardenStatueConfig> getSnowGardenStatueConfig() {
        return snowGardenStatueConfig;
    }

    public void setSnowGardenStatueConfig(Map<Integer, SnowGardenStatueConfig> snowGardenStatueConfig) {
        this.snowGardenStatueConfig = snowGardenStatueConfig;
    }

    public Map<Integer, SnowGardenVisitorConfig> getSnowGardenVisitorConfig() {
        return snowGardenVisitorConfig;
    }

    public void setSnowGardenVisitorConfig(Map<Integer, SnowGardenVisitorConfig> snowGardenVisitorConfig) {
        this.snowGardenVisitorConfig = snowGardenVisitorConfig;
        Map<Integer, List<Integer>> tempListMap = new HashMap<>();
        for (SnowGardenVisitorConfig visitorConfig : snowGardenVisitorConfig.values()) {
            int quality = visitorConfig.getQuality();
            List<Integer> list = tempListMap.get(quality);
            if (list == null) {
                list = new ArrayList<>();
                tempListMap.put(quality, list);
            }
            list.add(visitorConfig.getId());
        }
        qualityIdListMap = tempListMap;
    }

    public Map<Integer, SnowGardenShowConfig> getSnowGardenShowConfig() {
        return snowGardenShowConfig;
    }

    public void setSnowGardenShowConfig(Map<Integer, SnowGardenShowConfig> snowGardenShowConfig) {
        this.snowGardenShowConfig = snowGardenShowConfig;
        Map<Integer, Integer>  tempRandomShowMap = new HashMap<>();
        for (SnowGardenShowConfig showConfig : snowGardenShowConfig.values()) {
            tempRandomShowMap.put(showConfig.getQuality(), showConfig.getWeight());
        }
        this.randomShowMap = tempRandomShowMap;
    }

    public List<List<Integer>> getDice1() {
        return dice1;
    }

    public void setDice1(List<List<Integer>> dice1) {
        this.dice1 = dice1;
    }

    public List<List<Integer>> getDice2() {
        return dice2;
    }

    public void setDice2(List<List<Integer>> dice2) {
        this.dice2 = dice2;
    }
    public int randomVisitorConfigId(){
        int randomQuality = RandomHelper.getRandomKeyByWeight(randomShowMap);//随机品质
        List<Integer> list = qualityIdListMap.get(randomQuality);
        if (list == null) {
            return 0;
        }
        int index = random.nextInt(list.size());
        return list.get(index);
    }

    public String getRollReward() {
        return rollReward;
    }

    public void setRollReward(String rollReward) {
        this.rollReward = rollReward;
        this.rollRewardList = PropertyHelper.parseNewStringToPropertyList(rollReward);
    }

    public String getRollReward2() {
        return rollReward2;
    }

    public void setRollReward2(String rollReward2) {
        this.rollReward2 = rollReward2;
        this.rollRewardList2 = PropertyHelper.parseNewStringToPropertyList(rollReward2);
    }

    public List<List<Property>> getRollRewardList() {
        return rollRewardList;
    }

    public List<List<Property>> getRollRewardList2() {
        return rollRewardList2;
    }

    public long getPeriod1StartTime() {
        return period1StartTime;
    }

    public void setPeriod1StartTime(long period1StartTime) {
        this.period1StartTime = period1StartTime;
    }

    public long getPeriod1EndTime() {
        return period1EndTime;
    }

    public void setPeriod1EndTime(long period1EndTime) {
        this.period1EndTime = period1EndTime;
    }

    public long getPeriod2StartTime() {
        return period2StartTime;
    }

    public void setPeriod2StartTime(long period2StartTime) {
        this.period2StartTime = period2StartTime;
    }

    public long getPeriod2EndTime() {
        return period2EndTime;
    }

    public void setPeriod2EndTime(long period2EndTime) {
        this.period2EndTime = period2EndTime;
    }

    /**
     * 解析字符串转换时间
     */
    public static Calendar parseStringToDate(String s){
        List<String> Time = StringUtils.stringToStringList(s,"\\.");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.valueOf(Time.get(0)));
        calendar.set(Calendar.MONTH, Integer.valueOf(Time.get(1)) - 1);
        calendar.set(Calendar.DATE, Integer.valueOf(Time.get(2)));
        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(Time.get(3)));
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar;
    }

    // 地图长度
    public int getMapSize(){
        return snowGardenMapConfig.size();
    }

    // 通过建筑id获取建筑的积分
    public int getStatueScore(int statueId) {
        if (!snowGardenStatueConfig.containsKey(statueId)){
            return 0;
        }
        return snowGardenStatueConfig.get(statueId).getScore();
    }

    // 通过事件id获取事件参数
    public int getCombParam(int combId){
        SnowGardenCombConfig config = snowGardenCombConfig.get(combId);
        if (config == null){
            return 0;
        }
        return config.getParam();
    }

    // 通过事件id获取事件奖励参数
    public Property getCombReward(int combId){
        Property reward = new Property();
        SnowGardenCombConfig config = snowGardenCombConfig.get(combId);
        if (config == null){
            return reward;
        }
        return PropertyHelper.randomPropertyByListList(config.getRewardList());
    }

    // 通过位置id，获取该位置建筑的积分
    public int getScoreByIndex(int index){
        if (!snowGardenMapConfig.containsKey(index)){
            return 0;
        }
        int statueId = snowGardenMapConfig.get(index).getStatueId();
        return getStatueScore(statueId);
    }

    // 通过位置获取地块类型
    public int getIndexType(int index){
        return snowGardenMapConfig.get(index).getType();
    }

    /**
     * 判断指定事件是否匹配阶段 + roll点
     * @param combKey key
     * @param period 阶段
     * @param rollNum 投掷点数
     * @return bool
     */
    public boolean checkComb(int combKey, int period, int rollNum){
        return snowGardenCombConfig.get(combKey).getPeriod() == period && snowGardenCombConfig.get(combKey).getComb() == rollNum;
    }

    // 获取游客id
    public int getVisitorId(int id){
        return snowGardenVisitorConfig.get(id).getVisitorId();
    }

    // 获取游客积分
    public int getVisitorScore(int id){
        if (!snowGardenVisitorConfig.containsKey(id)){
            return 0;
        }
        return snowGardenVisitorConfig.get(id).getScore();
    }

    /**
     * 判断随机游客是否匹配品质 + roll点
     * @param key key
     * @param randomQuality 随机品质
     * @param rollNum 投掷点数
     * @return bool
     */
    public boolean checkVisitor(int key, int randomQuality, int rollNum){
        return snowGardenVisitorConfig.get(key).getQuality() == randomQuality && snowGardenVisitorConfig.get(key).getOccupation() == rollNum;
    }
}
